//
//  HTTPTool.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/6/29.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "HTTPTool.h"
#import "AFNetworking.h"
@implementation HTTPTool


/**
 *  网络请求get
 *
 *  @param url     请求url
 *  @param body    网络请求携带的body
 *  @param style   请求返回的数据格式
 *  @param success 成功调用的block
 *  @param failure 失败调用的block
 */

+ (void)get:(NSString *)url
       body:(id)body
    httpResult:(HTTPStyle)style
    success:(void (^)(id))success
    failure:(void (^)(NSError *))failure

{
    
    
    //1.获取AFN网络请求的管理类
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //2.判断返回的responseObject的数据类型.
    switch (HTTP) {
        case HTTP:
            //nsdata类型
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            
            //json类型
        case JSON:
            break;
            
            //xml类型
            case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
        default:
            break;
    }
    
    
    
    //3.让管理类调用get请求.
    [manager GET:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        
        //如果成功返回
        if (success) {
            
            success(responseObject);

        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        //如果失败返回error信息.
        if (failure) {
            failure(error);
        }
    }];
    
    
    
}


/**
 *  post请求
 *
 *  @param url       请求url
 *  @param body      网络请求携带的body
 *  @param style     网络请求返回的数据类型
 *  @param bodyStyle 网络请求body类型
 *  @param success   成功返回的block
 *  @param failure   失败返回的block
 */

+ (void)post:(NSString *)url
        body:(id)body
  httpResult:(HTTPStyle)style
requestBodyStyle:(RequestBodyStyle)bodyStyle
     success:(void(^)(id result))success
     failure:(void(^)(NSError *error)) failure


{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    switch (bodyStyle) {
        case BodyHTTP:
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
            case BodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
            
        default:
            break;
    }
    
    
    
    
    
    switch (style) {
        case HTTP:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
            
            case JSON:
            break;
            
            case XML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    
    
    
    
    [manager POST:url parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            
            failure(error);
            
        }
        
    }];
    
}












@end
